package com.zmykj.hik.hikService.Service.ISAPI;

import com.zmykj.hik.hikService.Common.CommonMethod;
import com.zmykj.hik.hikService.Common.HikStringUtils;
import com.zmykj.hik.hikService.Service.CMS.ECMS;
import com.zmykj.hik.hikService.Service.CMS.ISUPCMSByJNA.NET_EHOME_PTXML_PARAM;
import com.zmykj.hik.hikService.Common.CommonClass.StringPointer;

public class TransportISAPI {

    /**
     * 下发指令信息
     * @param lUserID 设备连接ID
     * @param sCmdType 方法类型
     * @param sReqUrl  链接URL
     * @param sInbound 输入信息
     * @param iTimeOut 超时时间
     * @param sOutputBuff 输出buffer
     * @param chinese 中文字符替换符（下发人员信息时此字段必填）
     * @return
     */
	public static boolean PassthroughISAPI(long lUserID, String sCmdType, String sReqUrl, String sInbound, int iTimeOut, StringBuffer sOutputBuff,String... chinese)
	{
		NET_EHOME_PTXML_PARAM struXmlCfg = new NET_EHOME_PTXML_PARAM();
		
		StringPointer pUrl     = new StringPointer(sReqUrl);
        pUrl.write();
        struXmlCfg.pRequestUrl = pUrl.getPointer();
        struXmlCfg.dwRequestUrlLen = sReqUrl.length();
        
        StringPointer pOutputBuff = new StringPointer(512 * 1024);
        pOutputBuff.write();
        struXmlCfg.pOutBuffer = pOutputBuff.getPointer();
        struXmlCfg.dwOutSize  = 512 * 1024;
        
        struXmlCfg.dwRecvTimeOut = iTimeOut;
        boolean bRet = false;

        if(sInbound==null || sInbound.equals(""))
        {
            sInbound = "";
        }

        if(sInbound.length() != 0)
		{
            StringPointer pInbound = null;
            if(HikStringUtils.isContainChinese(sInbound)){
                System.out.println("中文姓名==="+chinese[0]);
                 pInbound = HikStringUtils.hikStringZ(sInbound,chinese[0]);
            }else{
                 pInbound = new StringPointer(sInbound);
            }
            pInbound.write();
            struXmlCfg.pInBuffer = pInbound.getPointer();
            struXmlCfg.dwInSize  = pInbound.size();
		}

        if(sCmdType.equals("PUT"))
        {
            bRet = ECMS.GetCMSInstance().NET_ECMS_PutPTXMLConfig(lUserID, struXmlCfg);
        }
        else if(sCmdType.equals("POST"))
        {
            bRet = ECMS.GetCMSInstance().NET_ECMS_PostPTXMLConfig(lUserID, struXmlCfg);
        }
        else if(sCmdType.equals("DELETE"))
        {
            bRet = ECMS.GetCMSInstance().NET_ECMS_DeletePTXMLConfig(lUserID, struXmlCfg);
        }
        else if(sCmdType.equals("GET"))
        {
            bRet = ECMS.GetCMSInstance().NET_ECMS_GetPTXMLConfig(lUserID, struXmlCfg);
        }

        pOutputBuff.read();
        sOutputBuff.append(CommonMethod.byteToString(pOutputBuff.sData));

        if(bRet)
        {
        	CommonMethod.logRecord("INFO", "TransportISAPI Success");
        }
        else
        {
        	int dwErr = ECMS.GetCMSInstance().NET_ECMS_GetLastError();
        	CommonMethod.logRecord("ERROR", "TransportISAPI Error,errCode is:" + dwErr);
        }
        return bRet;
	}
}
